﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ThuatToanThongMinh
{
    public partial class Form_MaDiTuan : Form
    {
        public Form_MaDiTuan()
        {
            InitializeComponent();
        }
        public int KichThuoc;// Kich thuoc ban co
        public int ToaDoX;// Toa do X ban dau cua con co
        public int ToaDoY;// // Toa do Y ban dau cua con co
        private PictureBox[,] BanCo;
        private const int DoRong = 30;// do rong cua o co
        private Label[,] LB_Co;
        private bool Chay;
        private Co[] Vitri;
        /**-----------------------------------------------------------
        * Mo ta: Khoi tao ban co giao dien cung voi cac thanh phan khac
        * Ten phuong thuc: KhoiTao
        * Tham so: Khong co tham so
        * Gia tri tra ve: Khong co gia tri tra ve
        ---------------------------------------------------------*/
        public void KhoiTao()
        {

            label4.Text = KichThuoc.ToString() + " x " + KichThuoc.ToString();
            label5.Text = (ToaDoX + 1).ToString();
            label6.Text = (ToaDoY + 1).ToString();
            label10.Text = "";
            //Xu li ma di tuan
            madituan Ma = new madituan();
            Ma.Set(KichThuoc, ToaDoX, ToaDoY);
            Ma.KhoiTao();
            int dem = 1;
            Ma.Xuly(ref dem, ToaDoX, ToaDoY);
            ///////////
            Vitri = new Co[KichThuoc * KichThuoc];
            for (int i = 0; i < KichThuoc * KichThuoc; i++)
                Vitri[i] = new Co();
            Vitri = Ma.GetVT();
            /////////////////////////////
            LB_Co = new Label[2, KichThuoc];
            for (int i = 0; i < KichThuoc; i++)
            {
                //Ngang
                LB_Co[0, i] = new Label();
                LB_Co[0, i].Location = new Point(250 + DoRong * (i + 1) - 5, 9);
                LB_Co[0, i].Size = new Size(19, 13);
                LB_Co[0, i].Text = (i + 1).ToString();
                LB_Co[0, i].ForeColor = Color.Crimson;
                this.Controls.Add(LB_Co[0, i]);
                //Doc
                LB_Co[1, i] = new Label();
                LB_Co[1, i].Location = new Point(250, DoRong * (i + 1));
                LB_Co[1, i].Size = new Size(19, 13);
                LB_Co[1, i].Text = (i + 1).ToString();
                LB_Co[1, i].ForeColor = Color.Crimson;
                this.Controls.Add(LB_Co[1, i]);
            }
            BanCo = new PictureBox[KichThuoc, KichThuoc];
            for (int i = 0; i < KichThuoc; i++)
            {

                for (int j = 0; j < KichThuoc; j++)
                {
                    //Ve ban co
                    BanCo[i, j] = new PictureBox();
                    if (i % 2 != 0)
                    {
                        if (j % 2 != 0)
                        {
                            //BanCo[i, j].BackColor = Color.IndianRed;
                            BanCo[i, j].BackgroundImage = global::ThuatToanThongMinh.Properties.Resources.Nen_trang;
                        }
                        else
                        {
                            //BanCo[i, j].BackColor = Color.Indigo;
                            BanCo[i, j].BackgroundImage = global::ThuatToanThongMinh.Properties.Resources.Nen_do;
                        }
                    }
                    else if (j % 2 != 0)
                    {
                        //BanCo[i, j].BackColor = Color.Indigo;
                        BanCo[i, j].BackgroundImage = global::ThuatToanThongMinh.Properties.Resources.Nen_do;
                    }
                    else
                    {
                        //BanCo[i, j].BackColor = Color.IndianRed;
                        BanCo[i, j].BackgroundImage = global::ThuatToanThongMinh.Properties.Resources.Nen_trang;
                    }

                    if (i < 10)
                    {
                        if (j < 10)
                            BanCo[i, j].Name = "0" + i.ToString() + "0" + j.ToString();
                        else
                            BanCo[i, j].Name = "0" + i.ToString() + j.ToString();
                    }
                    else
                    {
                        if (j < 10)
                            BanCo[i, j].Name = i.ToString() + "0" + j.ToString();
                        else
                            BanCo[i, j].Name = i.ToString() + j.ToString();
                    };
                    BanCo[i, j].Location = new Point(270 + DoRong * i, 25 + DoRong * j);
                    BanCo[i, j].Size = new System.Drawing.Size(DoRong, DoRong);
                    BanCo[i, j].SizeMode = PictureBoxSizeMode.StretchImage;
                    this.Controls.Add(BanCo[i, j]);

                }
            }

            BanCo[ToaDoX, ToaDoY].Image = global::ThuatToanThongMinh.Properties.Resources.Ngua_do;

        }
        // Dua ban co ve rong ko co con co
        public void XoaNgua()
        {
            for (int i = 0; i < KichThuoc; i++)
            {
                for (int j = 0; j < KichThuoc; j++)
                {
                    BanCo[i, j].Image = null;
                }
            }
        }
        //Load form ban dau
        private void Form2_Load(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            KhoiTao();
            Chay = false;
            Width = 300 + DoRong * KichThuoc + 10;
            Height = 60 + DoRong * KichThuoc + 10;
            label10.Text = (ToaDoX + 1).ToString() + " , " + (ToaDoY + 1).ToString();
            button4.Enabled = false;
            Cursor = Cursors.Default;
        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Hide();
        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            Form F_1 = new ViTriMa();
            F_1.Show();
            Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //XuLy();
            if (comboBox1.Text == "")
                MessageBox.Show("Bạn chưa xác định thời gian chờ");
            else
            {
                button4.Enabled = true;
                if (Chay == false)
                {
                    Chay = true;
                    comboBox1.Enabled = false;
                    if (button1.Text == "Bắt Đầu")
                        XoaNgua();
                    BanCo[ToaDoX, ToaDoY].Image = global::ThuatToanThongMinh.Properties.Resources.Ngua_do;
                    button1.Text = "Tạm Ngưng";
                    if (comboBox1.Text == "0.5")
                        timer1.Interval = 500;
                    else
                    {
                        timer1.Interval = (int.Parse(comboBox1.Text)) * 1000;
                    }
                    timer1.Enabled = true;
                }
                else
                {
                    comboBox1.Enabled = true;
                    button1.Text = "Chạy Tiếp";
                    Chay = false;
                    timer1.Enabled = false;
                }
            }
        }
        private int dem = 1;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Chay == true)
            {

                label10.Text = (Vitri[dem].x + 1).ToString() + " , " + (Vitri[dem].y + 1).ToString();
                dem++;
                BanCo[Vitri[dem - 1].x, Vitri[dem - 1].y].Image = global::ThuatToanThongMinh.Properties.Resources.Ngua_trang;

                if (dem == KichThuoc * KichThuoc)
                {
                    BanCo[Vitri[dem - 1].x, Vitri[dem - 1].y].Image = global::ThuatToanThongMinh.Properties.Resources.Ngua_do;
                    timer1.Enabled = false;
                    dem = 1;
                    Chay = false;
                    comboBox1.Enabled = true;
                    button1.Text = "Bắt Đầu";
                    button4.Enabled = false;
                }

            }
        }

        private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Chay = false;
            if (dem > 1 && dem < KichThuoc * KichThuoc)
                button1.Text = "Chạy Tiếp";

            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "txt files (*.txt)|*.txt";
            if (save.ShowDialog() == DialogResult.OK)
            {
                FileStream fileStream = new FileStream(save.FileName, FileMode.Create, FileAccess.Write);
                StreamWriter streamWriter = new StreamWriter(fileStream);
                streamWriter.WriteLine("Kich thuoc ban co: " + KichThuoc.ToString() + " x " + KichThuoc.ToString());
                streamWriter.WriteLine("Toa do xuat phat: ( " + (ToaDoX + 1).ToString() + " , " + (ToaDoY + 1).ToString() + " )");
                streamWriter.WriteLine("Toa do cac diem di lan luot theo thu tu la:");
                for (int i = 1; i < KichThuoc * KichThuoc; i++)
                    streamWriter.WriteLine("( " + Vitri[i].x.ToString() + " , " + Vitri[i].x.ToString() + " )");
                streamWriter.WriteLine("Ket thuc...");
                streamWriter.Close();
                fileStream.Close();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            timer1.Interval = 1;
            comboBox1.Enabled = false;
            Chay = true;
            button1.Text = "Tạm Ngưng";
            timer1.Enabled = true;
            button4.Enabled = false;
        }
    }
}
